package com.punch.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.punch.common.dto.system.TeacherLectureDTO;
import com.punch.common.exception.BusinessException;
import com.punch.common.page.PageInfo;
import com.punch.common.query.syste.TeacherLectureQueryInfo;
import com.punch.common.simple.redis.utils.UserUtil;
import com.punch.common.tools.BasePage;
import com.punch.common.utils.ImageUtil;
import com.punch.common.vo.system.TeacherLectureVO;
import com.punch.system.exception.SysExceptionEnum;
import com.punch.system.mapper.TeacherLectureMapper;
import com.punch.system.model.TeacherLecture;
import com.punch.system.service.TeacherLectureService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.StringJoiner;

/**
 * <p>
 * 名师讲堂表 服务实现类
 * </p>
 *
 * @author wjj
 * @since 2020-12-14
 */
@Service
public class TeacherLectureServiceImpl extends ServiceImpl<TeacherLectureMapper, TeacherLecture> implements TeacherLectureService {

    /**
     * 名师讲堂列表
     *
     * @param queryInfo 查询条件
     * @return PageInfo<TeacherLectureVO>
     */
    @Override
    public PageInfo<TeacherLectureVO> listTeacherLecture(TeacherLectureQueryInfo queryInfo) {
        queryInfo.setSortStr("sort_by:asc");
        return BasePage.queryBasePage(queryInfo, params -> this.getBaseMapper().queryCount(params),
                params -> {
                    List<TeacherLectureVO> retList = getBaseMapper().queryList(params);
                    retList.forEach(teacherLectureVO -> {
                        if (teacherLectureVO.getStatus() == 0) {
                            teacherLectureVO.setUpdateTime(null);
                        }
                    });
                    return retList;
                });
    }

    /**
     * 保存名师讲堂
     *
     * @param teacherLectureDTO 提交表单
     * @return id
     */
    @Override
    public Integer saveTeacherLecture(TeacherLectureDTO teacherLectureDTO) {
        TeacherLecture teacherLecture = new TeacherLecture();
        BeanUtil.copyProperties(teacherLectureDTO, teacherLecture);
        Long userId = UserUtil.getLoginUserId();
        teacherLecture.setCreateId(userId);
        //处理讲堂封面图片前缀
        if (StrUtil.isNotEmpty(teacherLectureDTO.getCover())) {
            String img = ImageUtil.removePrefix(teacherLectureDTO.getCover());
            teacherLecture.setCover(img);
        }
        //处理介绍图片
        if (CollUtil.isEmpty(teacherLectureDTO.getImageList())) {
            teacherLecture.setIntroduceImg("");
        } else {
            StringJoiner sj = new StringJoiner(",");
            teacherLectureDTO.getImageList().forEach((img) -> sj.add(ImageUtil.removePrefix(img)));
            teacherLecture.setIntroduceImg(sj.toString());
        }
        if (!save(teacherLecture)) {
            throw new BusinessException(SysExceptionEnum.DATA_INSERT_EXCEPTION);
        }
        return teacherLecture.getId();
    }

    /**
     * 修改名师讲堂
     *
     * @param teacherLectureDTO 提交表单
     * @return id
     */
    @Override
    public Integer updateTeacherLecture(TeacherLectureDTO teacherLectureDTO) {
        TeacherLecture teacherLecture = new TeacherLecture();
        BeanUtil.copyProperties(teacherLectureDTO, teacherLecture);
        teacherLecture.setId(teacherLectureDTO.getLectureId());
        //处理介绍图片
        if (CollUtil.isEmpty(teacherLectureDTO.getImageList())) {
            teacherLecture.setIntroduceImg("");
        } else {
            StringJoiner sj = new StringJoiner(",");
            teacherLectureDTO.getImageList().forEach((img) -> sj.add(ImageUtil.removePrefix(img)));
            teacherLecture.setIntroduceImg(sj.toString());
        }
        //处理名师讲堂图片前缀
        if (StrUtil.isNotEmpty(teacherLectureDTO.getCover())) {
            String img = ImageUtil.removePrefix(teacherLectureDTO.getCover());
            teacherLecture.setCover(img);
        }
        if (!updateById(teacherLecture)) {
            throw new BusinessException(SysExceptionEnum.DATA_UPDATE_EXCEPTION);
        }
        return teacherLecture.getId();
    }

    /**
     * 名师讲堂详情
     *
     * @param lectureId lectureId
     * @return TeacherLectureVO
     */
    @Override
    public TeacherLectureVO getTeacherLecture(Integer lectureId) {
        TeacherLecture teacherLectureEX = getById(lectureId);
        Optional.ofNullable(teacherLectureEX).orElseThrow(() -> new BusinessException(SysExceptionEnum.DATA_NOT_EXIST));
        TeacherLectureVO teacherLectureVO = new TeacherLectureVO();
        BeanUtil.copyProperties(teacherLectureEX, teacherLectureVO);
        //讲堂介绍
        if (StrUtil.isNotEmpty(teacherLectureEX.getIntroduceImg())) {
            String[] split = teacherLectureEX.getIntroduceImg().split(",");
            List<String> imgList = new ArrayList<>();
            for (String img : split) {
                imgList.add(ImageUtil.addPrefix(img));
            }
            teacherLectureVO.setImageList(imgList);
        }
        //讲堂封面图片
        if (StrUtil.isNotEmpty(teacherLectureEX.getCover())) {
            teacherLectureVO.setCover(ImageUtil.addPrefix(teacherLectureEX.getCover()));
        }
        teacherLectureVO.setLectureId(lectureId);
        return teacherLectureVO;
    }

    /**
     * 删除名师讲堂
     *
     * @param lectureId lectureId
     * @return id
     */
    @Override
    public Integer deleteTeacherLecture(Integer lectureId) {
        TeacherLecture teacherLectureEX = getById(lectureId);
        Optional.ofNullable(teacherLectureEX).orElseThrow(() -> new BusinessException(SysExceptionEnum.DATA_NOT_EXIST));
        //已经展示的不能删除,需要下架
        if (teacherLectureEX.getStatus() == 1) {
            throw new BusinessException(SysExceptionEnum.STATUS_ERROR);
        }
        if (!removeById(lectureId)) {
            throw new BusinessException(SysExceptionEnum.DATA_DELETION_EXCEPTION);
        }
        return lectureId;
    }

    /**
     * 修改名师讲堂状态
     *
     * @param lectureId lectureId
     * @param status    状态 0-不显示 1-显示
     * @return teacherId
     */
    @Override
    public Integer updateTeacherLectureStatus(Integer lectureId, Integer status) {
        TeacherLecture teacherLectureEX = getById(lectureId);
        Optional.ofNullable(teacherLectureEX).orElseThrow(() -> new BusinessException(SysExceptionEnum.DATA_NOT_EXIST));
        if (teacherLectureEX.getStatus().equals(status)) {
            throw new BusinessException(SysExceptionEnum.STATUS_ERROR);
        }
        TeacherLecture teacherLecture = new TeacherLecture();
        teacherLecture.setStatus(status);
        teacherLecture.setId(lectureId);
        if (!updateById(teacherLecture)) {
            throw new BusinessException(SysExceptionEnum.DATA_UPDATE_EXCEPTION);
        }
        return lectureId;
    }
}
